package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


import VideoTools.BufferAnalyse;
import VideoTools.Direction;
import VideoTools.Movement;

public class Settings extends JFrame{

	
	private JTabbedPane onglets;
	
	public Settings (){
		this.setTitle("Settings");
		this.setSize(500,500);
	
		
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		onglets = new JTabbedPane();
		onglets.addTab("Simulation",new SettingsMovement());
		onglets.addTab("BufferAnalyse",new SettingsBA());
		onglets.addTab("Analyse",new SettingsAloc());
		onglets.setSelectedIndex(0);
		getContentPane().add(onglets);
		this.setVisible(true);
	}
	
	public static void main (String[] args){
		new Settings();
	}

	
}

class SettingsMovement extends JPanel implements ActionListener, KeyListener {
	
	private static final String WAIT_FOR_STRIKING_A_KEY  = "Strike a key";
	
	private JPanel keyboard = new JPanel();
	private JPanel mouse = new JPanel();
	private JLabel keyboardtext = new JLabel("- Keyboard Simulation -");
	private JLabel mousetext = new JLabel("- Mouse Simulation -");
	
	private JLabel etat = new JLabel("etat");
	private JPanel bouton = new JPanel();
	
	private JPanel numberof = new JPanel();
	private JLabel number_of_movement=new JLabel("frames par mouvement :");
	private JTextField number_of_movement_text=new JTextField(Integer.toString(Movement.NB_FRAME_FOR_ONE_SIMULATION));
	private JButton number_of_movement_valid= new JButton("valider");
	
	private JLabel hautlabel=new JLabel("Haut");
	private JLabel baslabel=new JLabel("Bas");
	private JLabel droitelabel=new JLabel("Droite");
	private JLabel gauchelabel=new JLabel("Gauche");
	private JLabel hautdroitelabel=new JLabel("Haut-Gauche");
	private JLabel basdroitelabel=new JLabel("Bas-Gauche");
	private JLabel basgauchelabel=new JLabel("Bas-Droite");
	private JLabel hautgauchelabel=new JLabel("Haut-Droite");
	
	private JTextField hautvalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UP_1));
	private JTextField hautvalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UP_2));
	private JTextField basvalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWN_1));
	private JTextField basvalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWN_2));
	private JTextField droitevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_RIGHT_1));
	private JTextField droitevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_RIGHT_2));
	private JTextField gauchevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_LEFT_1));
	private JTextField gauchevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_LEFT_2));
	private JTextField hautgauchevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UPLEFT_1));
	private JTextField hautgauchevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UPLEFT_2));
	private JTextField basgauchevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWNLEFT_1));
	private JTextField basgauchevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWNLEFT_2));
	private JTextField hautdroitevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UPRIGHT_1));
	private JTextField hautdroitevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_UPRIGHT_2));
	private JTextField basdroitevalue1 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWNRIGHT_1));
	private JTextField basdroitevalue2 = new JTextField(Integer.toString(Movement.SIMULATION_KEY_DOWNRIGHT_2));
	
	private JButton hautButton1 = new JButton("Changer");
	private JButton hautButton2 = new JButton("Changer");
	private JButton basButton1 = new JButton("Changer");
	private JButton basButton2 = new JButton("Changer");
	private JButton droiteButton1 = new JButton("Changer");
	private JButton droiteButton2 = new JButton("Changer");
	private JButton gaucheButton1 = new JButton("Changer");
	private JButton gaucheButton2 = new JButton("Changer");
	private JButton hautdroiteButton1 = new JButton("Changer");
	private JButton hautdroiteButton2 = new JButton("Changer");
	private JButton basgaucheButton1 = new JButton("Changer");
	private JButton basgaucheButton2 = new JButton("Changer");
	private JButton basdroiteButton1 = new JButton("Changer");
	private JButton basdroiteButton2 = new JButton("Changer");
	private JButton hautgaucheButton1 = new JButton("Changer");
	private JButton hautgaucheButton2 = new JButton("Changer");
	
	private JTextField memory=null;
	private boolean attente=false;
	
	public SettingsMovement(){
		
		keyboardtext.setForeground(Color.red);
		mousetext.setForeground(Color.red);
		
		hautvalue1.setEnabled(false);
		hautvalue1.setEnabled(false);
		hautvalue2.setEnabled(false);
		basvalue1.setEnabled(false);
		basvalue2.setEnabled(false);
		droitevalue1.setEnabled(false);
		droitevalue2.setEnabled(false);
		gauchevalue1.setEnabled(false);
		gauchevalue2.setEnabled(false);
		hautgauchevalue1.setEnabled(false);
		hautgauchevalue2.setEnabled(false);
		basgauchevalue1.setEnabled(false);
		basgauchevalue2.setEnabled(false);
		hautdroitevalue1.setEnabled(false);
		hautdroitevalue2.setEnabled(false);
		basdroitevalue1.setEnabled(false);
		basdroitevalue2.setEnabled(false);
		
		bouton.setBackground(Color.gray);
		bouton.setLayout(new GridLayout(8,5));
		bouton.add(hautlabel);
		bouton.add(hautvalue1);
		bouton.add(hautButton1);
		bouton.add(hautvalue2);
		bouton.add(hautButton2);
		bouton.add(baslabel);
		bouton.add(basvalue1);
		bouton.add(basButton1);
		bouton.add(basvalue2);
		bouton.add(basButton2);
		bouton.add(droitelabel);
		bouton.add(droitevalue1);
		bouton.add(droiteButton1);
		bouton.add(droitevalue2);
		bouton.add(droiteButton2);
		bouton.add(gauchelabel);
		bouton.add(gauchevalue1);
		bouton.add(gaucheButton1);
		bouton.add(gauchevalue2);
		bouton.add(gaucheButton2);
		bouton.add(hautgauchelabel);
		bouton.add(hautgauchevalue1);
		bouton.add(hautgaucheButton1);
		bouton.add(hautgauchevalue2);
		bouton.add(hautgaucheButton2);
		bouton.add(basdroitelabel);
		bouton.add(basdroitevalue1);
		bouton.add(basdroiteButton1);
		bouton.add(basdroitevalue2);
		bouton.add(basdroiteButton2);
		bouton.add(hautdroitelabel);
		bouton.add(hautdroitevalue1);
		bouton.add(hautdroiteButton1);
		bouton.add(hautdroitevalue2);
		bouton.add(hautdroiteButton2);
		bouton.add(basgauchelabel);
		bouton.add(basgauchevalue1);
		bouton.add(basgaucheButton1);
		bouton.add(basgauchevalue2);
		bouton.add(basgaucheButton2);
		
		numberof.setBackground(Color.gray);
		numberof.setLayout(new GridLayout(1,3));
		numberof.add(number_of_movement);
		numberof.add(number_of_movement_text);
		numberof.add(number_of_movement_valid);
		
		keyboard.setLayout(new BorderLayout());
		keyboard.add(keyboardtext,BorderLayout.NORTH);
		keyboard.add(bouton,BorderLayout.CENTER);
		keyboard.add(numberof,BorderLayout.SOUTH);
		
		mouse.setLayout(new BorderLayout());
		mouse.add(mousetext,BorderLayout.NORTH);
		
		
		this.setLayout(new BorderLayout());
		this.add(keyboard,BorderLayout.NORTH);
		this.add(mouse,BorderLayout.CENTER);
		this.add(etat,BorderLayout.SOUTH);
		
		hautButton1.addActionListener(this);
		hautButton2.addActionListener(this);
		basButton1.addActionListener(this);
		basButton2.addActionListener(this);
		gaucheButton1.addActionListener(this);
		gaucheButton2.addActionListener(this);
		droiteButton1.addActionListener(this);
		droiteButton2.addActionListener(this);
		hautdroiteButton1.addActionListener(this);
		hautdroiteButton2.addActionListener(this);
		basgaucheButton1.addActionListener(this);
		basgaucheButton2.addActionListener(this);
		hautgaucheButton1.addActionListener(this);
		hautgaucheButton2.addActionListener(this);
		basdroiteButton1.addActionListener(this);
		basdroiteButton2.addActionListener(this);
		
		number_of_movement_valid.addActionListener(this);
		
	}

	public void actionPerformed(ActionEvent arg0) {
		if (memory==null){
			if (arg0.getSource()==hautButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautvalue1;
				this.addKeyListener(this);
				hautvalue1.setEnabled(true);
			}
			if (arg0.getSource()==basButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basvalue1;
				this.addKeyListener(this);
				basvalue1.setEnabled(true);
			}
			if (arg0.getSource()==droiteButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = droitevalue1;
				this.addKeyListener(this);
				droitevalue1.setEnabled(true);
			}
			if (arg0.getSource()==gaucheButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = gauchevalue1;
				this.addKeyListener(this);
				gauchevalue1.setEnabled(true);
			}
			if (arg0.getSource()==hautdroiteButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautdroitevalue1;
				this.addKeyListener(this);
				hautdroitevalue1.setEnabled(true);
			}
			if (arg0.getSource()==hautgaucheButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautgauchevalue1;
				this.addKeyListener(this);
				hautgauchevalue1.setEnabled(true);
			}
			if (arg0.getSource()==basgaucheButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basgauchevalue1;
				this.addKeyListener(this);
				basgauchevalue1.setEnabled(true);
			}
			if (arg0.getSource()==basdroiteButton1){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basdroitevalue1;
				this.addKeyListener(this);
				basdroitevalue1.setEnabled(true);
			}
			if (arg0.getSource()==hautButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautvalue2;
				this.addKeyListener(this);
				hautvalue2.setEnabled(true);
			}
			if (arg0.getSource()==basButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basvalue2;
				this.addKeyListener(this);
				basvalue2.setEnabled(true);
			}
			if (arg0.getSource()==droiteButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = droitevalue2;
				this.addKeyListener(this);
				droitevalue2.setEnabled(true);
			}
			if (arg0.getSource()==gaucheButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = gauchevalue2;
				this.addKeyListener(this);
				gauchevalue2.setEnabled(true);
			}
			if (arg0.getSource()==hautdroiteButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautdroitevalue2;
				this.addKeyListener(this);
				hautdroitevalue2.setEnabled(true);
			}
			if (arg0.getSource()==hautgaucheButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = hautgauchevalue2;
				this.addKeyListener(this);
				hautgauchevalue2.setEnabled(true);
			}
			if (arg0.getSource()==basgaucheButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basgauchevalue2;
				this.addKeyListener(this);
				basgauchevalue2.setEnabled(true);
			}
			if (arg0.getSource()==basdroiteButton2){
				etat.setText(WAIT_FOR_STRIKING_A_KEY);
				this.memory = basdroitevalue2;
				this.addKeyListener(this);
				basdroitevalue2.setEnabled(true);
			}
			
			if (arg0.getSource()==number_of_movement_valid){
				try {
					Movement.NB_FRAME_FOR_ONE_SIMULATION=Integer.parseInt(number_of_movement_text.getText());
				}
				catch (NumberFormatException n){
					number_of_movement_text.setText(Integer.toString(Movement.NB_FRAME_FOR_ONE_SIMULATION));
				}
			}
		}
		this.requestFocus();
	}

	public void keyPressed(KeyEvent arg0) {
		etat.setText("etat ");
		this.memory.setText(Integer.toString(arg0.getKeyCode()));
		this.memory.setEnabled(false);
		this.memory=null;
		saveAll();
		this.removeKeyListener(this);
		
	}

	public void keyReleased(KeyEvent arg0) {
		
		
	}

	public void keyTyped(KeyEvent arg0) {
		
	}
	
	
	private void saveAll(){
		Movement.SIMULATION_KEY_UP_1=Integer.parseInt(hautvalue1.getText());
		Movement.SIMULATION_KEY_UP_2=Integer.parseInt(hautvalue2.getText());
		Movement.SIMULATION_KEY_DOWN_1=Integer.parseInt(basvalue1.getText());
		Movement.SIMULATION_KEY_DOWN_2=Integer.parseInt(basvalue2.getText());
		Movement.SIMULATION_KEY_RIGHT_1=Integer.parseInt(droitevalue1.getText());
		Movement.SIMULATION_KEY_RIGHT_2=Integer.parseInt(droitevalue2.getText());
		Movement.SIMULATION_KEY_LEFT_1=Integer.parseInt(gauchevalue1.getText());
		Movement.SIMULATION_KEY_LEFT_2=Integer.parseInt(gauchevalue2.getText());
		Movement.SIMULATION_KEY_UPLEFT_1=Integer.parseInt(hautgauchevalue1.getText());
		Movement.SIMULATION_KEY_UPLEFT_2=Integer.parseInt(hautgauchevalue2.getText());
		Movement.SIMULATION_KEY_DOWNLEFT_1=Integer.parseInt(basgauchevalue1.getText());
		Movement.SIMULATION_KEY_DOWNLEFT_2=Integer.parseInt(basgauchevalue2.getText());
		Movement.SIMULATION_KEY_UPRIGHT_1=Integer.parseInt(hautdroitevalue1.getText());
		Movement.SIMULATION_KEY_UPRIGHT_2=Integer.parseInt(hautdroitevalue2.getText());
		Movement.SIMULATION_KEY_DOWNRIGHT_1=Integer.parseInt(basdroitevalue1.getText());
		Movement.SIMULATION_KEY_DOWNRIGHT_2=Integer.parseInt(basdroitevalue2.getText());
	}
}


class SettingsBA extends JPanel implements ActionListener, ChangeListener {

	private JPanel paneltotal = new JPanel();
	
	private JLabel labelpaneltotal = new JLabel(" - Settings - ");
	private JPanel panelJslider = new JPanel();
	private JLabel redRangeLabel = new JLabel("red range : ");
	private JLabel blueRangeLabel = new JLabel("blue range : ");
	private JLabel greenRangeLabel = new JLabel("green range : ");
	private JSlider redRange;
	private JSlider greenRange;
	private JSlider blueRange;
	
	private JPanel numberof = new JPanel();
	private JLabel number_of_point=new JLabel("nombre de point minimum :");
	private JTextField number_of_point_text=new JTextField(Integer.toString(BufferAnalyse.MIN_NUMBER_OF_POINT));
	private JButton number_of_point_valid= new JButton("valider");
	private JLabel radius_window=new JLabel("diametre de la fenetre :");
	private JTextField radius_window_text=new JTextField(Integer.toString(BufferAnalyse.SIZE_RADIUS_WINDOWS));
	private JButton radius_window_valid= new JButton("valider");
	
	public SettingsBA(){
		
		labelpaneltotal.setForeground(Color.red);
		
		redRange = new JSlider(1,128,BufferAnalyse.redRange);
		greenRange = new JSlider(1,128,BufferAnalyse.greenRange);
		blueRange = new JSlider(1,128,BufferAnalyse.blueRange);
		
		redRange.setToolTipText("redRange");
		greenRange.setToolTipText("greenRange");
		blueRange.setToolTipText("blueRange");
		
		panelJslider.setBackground(Color.gray);
		panelJslider.setLayout(new GridLayout(3,2));
		panelJslider.add(redRangeLabel);
		panelJslider.add(redRange);
		panelJslider.add(greenRangeLabel);
		panelJslider.add(greenRange);
		panelJslider.add(blueRangeLabel);
		panelJslider.add(blueRange);
		
		numberof.setBackground(Color.gray);
		numberof.setLayout(new GridLayout(2,3));
		numberof.add(number_of_point);
		numberof.add(number_of_point_text);
		numberof.add(number_of_point_valid);
		numberof.add(radius_window);
		numberof.add(radius_window_text);
		numberof.add(radius_window_valid);
		
		paneltotal.setLayout(new BorderLayout());
		paneltotal.add(labelpaneltotal,BorderLayout.NORTH);
		paneltotal.add(panelJslider,BorderLayout.CENTER);
		paneltotal.add(numberof,BorderLayout.SOUTH);
		
		this.setLayout(new BorderLayout());
		this.add(paneltotal,BorderLayout.NORTH);
		
		
		redRange.addChangeListener(this);
		blueRange.addChangeListener(this);
		greenRange.addChangeListener(this);
		number_of_point_valid.addActionListener(this);
		radius_window_valid.addActionListener(this);
		
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource()==number_of_point_valid){
			try {
				BufferAnalyse.MIN_NUMBER_OF_POINT=Integer.parseInt(number_of_point_text.getText());
			}
			catch (NumberFormatException n){
				number_of_point_text.setText(Integer.toString(BufferAnalyse.MIN_NUMBER_OF_POINT));
			}
		}
		if (arg0.getSource()==radius_window_valid){
			try {
				BufferAnalyse.SIZE_RADIUS_WINDOWS=Integer.parseInt(radius_window_text.getText());
			}
			catch (NumberFormatException n){
				radius_window_text.setText(Integer.toString(BufferAnalyse.SIZE_RADIUS_WINDOWS));
			}
		}
	}

	public void stateChanged(ChangeEvent arg0) {
		if (arg0.getSource()==redRange){
			BufferAnalyse.redRange = redRange.getValue();
		}
		if (arg0.getSource()==blueRange){
			BufferAnalyse.blueRange = blueRange.getValue();
		}
		if (arg0.getSource()==greenRange){
			BufferAnalyse.greenRange = greenRange.getValue();
		}
		
	}
}


class SettingsAloc extends JPanel implements ActionListener {

	public SettingsAloc(){
	
	}
	
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}



}